home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 982 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.6 KB  |  109 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: anhaeupl@late.e-technik.uni-erlangen.de (Bernd Anhaeupl)
  3. Newsgroups: comp.std.c++
  4. Subject: dynamic_cast<void*>
  5. Date: 06 Apr 1996 11:32:28 PST
  6. Organization: LATE, Uni. Erlangen-Nuernberg, Germany
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4k1a4r$dim@rznews.rrze.uni-erlangen.de>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 4 Apr 1996 20:06:51 GMT
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBVAwUBMWbGzUy4NqrwXLNJAQEiCwIAjpATo3FqzWAnKp7KNq6CMUYZbdIo/fi/
  13.     KGzf50WaH6qvwF2Xq0fJX6Wg2gyrApWETIcSkDkDI3mwx9Hl8mS3Fg==
  14.     =m7Ik
  15. Originator: austern@isolde.mti.sgi.com
  16.  
  17. Section 5.2.6 paragraph 7 of the April 95 WP states, that for
  18. polymorphic types, the result of a dynamic_cast<void*>(v) "is a
  19. pointer to the complete object (12.6.2) pointed to by v". Now 
  20. section 12.6.2 does not define the term "complete object", which
  21. actually happens in section 1.4 paragraph 2, but the term 
  22. "most derived class". This makes a differnce, if we pass a pointer
  23. to a data member subobject (and not a base class subobject) to
  24. dynamic_cast. According to 12.6.2, we would get the address of that
  25. data member, according to 1.4, the address of the containing 
  26. object of that data member.
  27.  
  28. Please note, that the term "complete object (12.6.2)" is also used 
  29. in section 5.2.7, where the typeid expression is defined. Therefore 
  30. I suppose, that in 5.2.6 and 5.2.7 "most derived object" is meant 
  31. instead of "complete object" in both sections. Is this correct?
  32.  
  33. Example:
  34.  
  35. #include <typeinfo>
  36. #include <iostream>
  37.  
  38. class foo1{
  39. public:
  40.   foo1(){};
  41.   virtual ~foo1(){}
  42. };
  43.  
  44. class foo2{
  45. public:
  46.   foo2(){};
  47.   virtual ~foo2(){}
  48. };
  49.  
  50. class bar:public foo1, public foo2{
  51. public:
  52.   foo1 f1m;
  53.   foo2 f2m;
  54.   bar(){};
  55.   virtual ~bar(){}
  56. };
  57.  
  58. int main()
  59. {
  60.   bar    b;
  61.   foo1 & f1=b;
  62.   foo2 & f2=b;
  63.   foo1 & f1m=b.f1m;
  64.   foo2 & f2m=b.f2m;
  65.  
  66.   cout << "  &b == " << &b   
  67.     << "  dynamic_cast<void*>(  &b) == " << dynamic_cast<void*>(&b) << endl;
  68.   cout << " &f1 == " << &f1
  69.     << "  dynamic_cast<void*>( &f1) == " << dynamic_cast<void*>(&f1) << endl;
  70.   cout << " &f2 == " << &f2
  71.     << "  dynamic_cast<void*>( &f2) == " << dynamic_cast<void*>(&f2) << endl;
  72.   cout << "&f1m == " << &f1m
  73.     << "  dynamic_cast<void*>(&f1m) == " << dynamic_cast<void*>(&f1m) << endl;
  74.   cout << "&f2m == " << &f2m
  75.     << "  dynamic_cast<void*>(&f2m) == " << dynamic_cast<void*>(&f2m) << endl;
  76.   return 0;
  77. }
  78.   
  79.  
  80. Interpreting 5.2.6 in the sense of "most derived object" would yield 
  81. something like (at least gcc 2.7.0 does it in this way)
  82.  
  83.   &b == 0xbffffaa8  dynamic_cast<void*>(  &b) == 0xbffffaa8
  84.  &f1 == 0xbffffaa8  dynamic_cast<void*>( &f1) == 0xbffffaa8
  85.  &f2 == 0xbffffaac  dynamic_cast<void*>( &f2) == 0xbffffaa8
  86. &f1m == 0xbffffab0  dynamic_cast<void*>(&f1m) == 0xbffffab0
  87. &f2m == 0xbffffab4  dynamic_cast<void*>(&f2m) == 0xbffffab4
  88.  
  89. where "complete object" should result in
  90.  
  91.   &b == 0xbffffaa8  dynamic_cast<void*>(  &b) == 0xbffffaa8
  92.  &f1 == 0xbffffaa8  dynamic_cast<void*>( &f1) == 0xbffffaa8
  93.  &f2 == 0xbffffaac  dynamic_cast<void*>( &f2) == 0xbffffaa8
  94. &f1m == 0xbffffab0  dynamic_cast<void*>(&f1m) == 0xbffffaa8
  95. &f2m == 0xbffffab4  dynamic_cast<void*>(&f2m) == 0xbffffaa8
  96.  
  97. -- 
  98. Bernd Anhaeupl            Tel.:  +49 9131 857787
  99. LATE - Uni Erlangen            
  100. Cauerstr. 7            Email: anhaeupl@late.e-technik.uni-erlangen.de
  101. 91058 Erlangen
  102. ---
  103. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  104.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  105.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  106.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  107.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  108. ]
  109.